﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using DAL;

namespace BLL
{
    public class PunchManager
    {

        private static EFHelper<OASystemEntities> _efHelper = new EFHelper<OASystemEntities>();

        /// <summary>
        /// 查询考勤标准表集合
        /// </summary>
        /// <returns></returns>
        public static List<OA_PunchCard> QueryList()
        {
            try
            {
                return _efHelper.QueryList<OA_PunchCard>(null);
            }
            catch (Exception ex)
            {
                return new List<OA_PunchCard>();
            }

        }

        /// <summary>
        /// 查询考勤表集合
        /// </summary>
        /// <returns></returns>
        public static List<OA_WorkAttendance> QueryListByWork()
        {
            try
            {
                //考虑是超级管理员还是非超管
                //获取当前登录用户
                var user = System.Web.HttpContext.Current.Session["info"] as Model.OA_UserAccount;
                if (user.IsSuper)
                    return _efHelper.QueryList<OA_WorkAttendance>(null);
                else
                    return _efHelper.QueryList<OA_WorkAttendance>(w => w.UserId == user.Id).ToList();
            }
            catch (Exception ex)
            {
                return new List<OA_WorkAttendance>();
            }

        }



        /// <summary>
        /// 新增考勤标准
        /// </summary>
        /// <param name="pc"></param>
        /// <returns></returns>
        public bool Insert(OA_PunchCard pc)
        {
            try
            {
                pc.IsEable = false;
                pc.InsertTime = DateTime.Now;
                pc.UpdateTime = DateTime.Now;

                var num = _efHelper.Insert<OA_PunchCard>(pc);
                if (num == 1)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 启用或禁用该考勤标准
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public ResultMessage UpdateIsEnable(int Id, bool result)
        {
            try
            {
                var Entity = _efHelper.GetInfo<OA_PunchCard>(p => p.Id == Id);
                if (Entity.Id != 0)
                {

                    Entity.IsEable = result;


                    if (result)
                    {
                        var otherPunchCardList = _efHelper.QueryList<OA_PunchCard>(p => p.Id != Id).ToList();
                        if (otherPunchCardList.Count != 0)
                        {
                            foreach (var item in otherPunchCardList)
                            {
                                item.IsEable = false;
                                _efHelper.Modify<OA_PunchCard>(item);
                            }
                        }
                    }



                }
                var num = _efHelper.Modify<OA_PunchCard>(Entity);
                if (num == 1)
                    return new ResultMessage(true, "操作成功！");
                else
                    return new ResultMessage(false, "操作失败！");
            }
            catch (Exception ex)
            {
                return new ResultMessage(false, ex.Message);

            }
        }


        #region 暂时标注
        ///// <summary>
        ///// 考勤打卡
        ///// </summary>
        ///// <returns></returns>
        //public ResultMessage CheckWork()
        //{
        //    try
        //    {
        //        //获取当前登录员工账号信息
        //        var userAccount = System.Web.HttpContext.Current.Session["info"] as Model.OA_UserAccount;
        //        //获取当前考勤标准数据
        //        var punch = _efHelper.QueryList<OA_PunchCard>(p => p.IsEable).First();

        //        //1、查询该等了员工今天是否打卡，如果打了卡，是上午打卡还是下午

        //        var punchEntity = _efHelper.SqlQuery<OA_WorkAttendance>(string.Format("select * from OA_WorkAttendance where UserId={0} and DATEDIFF(day,InsertTime,GETDATE())=0", userAccount.Id)).FirstOrDefault();

        //        //_efHelper.GetInfo<OA_WorkAttendance>(W => W.UserId == userAccount.Id && Convert.ToDateTime(W.InsertTime).ToString("yyyy-MM-dd").Equals(DateTime.Now.ToString("yyyy-MM-dd")));
        //        if (punchEntity == null)
        //        {
        //            //该员工今天第一次上班打卡  a、迟到  b、正常上班
        //            OA_WorkAttendance wa = new OA_WorkAttendance()
        //            {
        //                StartTime = DateTime.Now,
        //                EndTime = null,
        //                PunchCardId = punch.Id,
        //                InsertTime = DateTime.Now,
        //                UpdateTime = DateTime.Now,
        //                UserId = userAccount.Id
        //            };
        //            var num = _efHelper.Insert<OA_WorkAttendance>(wa);
        //            if (num == 1)
        //                return new ResultMessage(true, "谢谢，已签到！");
        //            else
        //                return new ResultMessage(false, "签到失败！");

        //        }
        //        else
        //        {
        //            //这个时候只需考虑下班打卡情况  a、早退  b、正常下班
        //            punchEntity.EndTime = DateTime.Now;
        //            var num = _efHelper.Modify<OA_WorkAttendance>(punchEntity);
        //            if (num == 1)
        //                return new ResultMessage(true, "谢谢，已签到！");
        //            else
        //                return new ResultMessage(false, "签到失败！");
        //        }

        //    }
        //    catch (Exception ex)
        //    {
        //        return new ResultMessage(false, ex.Message);
        //    }
        //} 
        #endregion


        /// <summary>
        /// 打卡
        /// </summary>
        /// <returns></returns>
        public ResultMessage CheckWork()
        {
            try
            {

                //获取当前登录用户
                var user = System.Web.HttpContext.Current.Session["info"] as Model.OA_UserAccount;

                if (user == null)
                    return new ResultMessage(false, "未检测登录状态！！！");

                //获取上班打卡标准
                var entity = _efHelper.QueryList<OA_PunchCard>(p => p.IsEable).FirstOrDefault();

                //如何判断该登录员工是否是第一次打卡
                var workEntity = _efHelper.SqlQuery<OA_WorkAttendance>(string.Format(@"select * from OA_WorkAttendance where UserId={0}
  and DATEDIFF(day,InsertTime,GETDATE())=0", user.Id)).FirstOrDefault();

                if (workEntity == null)
                {
                    //上班打卡   打卡结果：迟到  正常
                    _efHelper.Insert<OA_WorkAttendance>(new OA_WorkAttendance()
                    {
                        PunchCardId = entity.Id,
                        StartTime = DateTime.Now,
                        UserId = user.Id,
                        InsertTime = DateTime.Now,
                        UpdateTime = DateTime.Now
                    });

                    return new ResultMessage(true, "谢谢，成功签到");

                }
                else
                {

                    //下班时间打卡   打卡结果：早退  正常
                    workEntity.EndTime = DateTime.Now;
                    _efHelper.Modify<OA_WorkAttendance>(workEntity);
                    return new ResultMessage(true, "谢谢，成功签到");
                }


            }
            catch (Exception ex)
            {
                return new ResultMessage(false, ex.Message);
            }
        }


        public List<Group> Query()
        {
            try
            {
                string sql = @"select d.Name as name,COUNT(UserId)as  y from OA_UserBasic 
left  join OA_Department d on DepartmentId=d.Id

  group by d.Name";

                return _efHelper.SqlQuery<Group>(sql);
            }
            catch (Exception)
            {

                throw;
            }

        }

    }
}
